package com.sjz.myfrist;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

/***
 * 音视频播放器
 */
public class MusicAndVideoActivity extends AppCompatActivity implements View.OnClickListener {

    private MediaPlayer mediaPlayer = new MediaPlayer();
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.musicandvideo);


        //音频
        Button play = (Button) findViewById(R.id.playMusic);
        Button pause = (Button) findViewById(R.id.pauseMusic);
        Button stop = (Button) findViewById(R.id.stopMusic);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);

        //视频
        videoView = (VideoView) findViewById(R.id.video_view);
        Button playVideo = (Button) findViewById(R.id.playVideo);
        Button pauseVideo = (Button) findViewById(R.id.pauseVideo);
        Button replay = (Button) findViewById(R.id.replayVideo);

        if (ContextCompat.checkSelfPermission(MusicAndVideoActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MusicAndVideoActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            initMediaPlayer(); // 初始化MediaPlayer
            initVideoPath(); // 初始化MediaPlayer
        }

    }

    //初始化视频
    private void initVideoPath() {
        File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
        videoView.setVideoPath(file.getPath()); // 指定视频文件的路径
    }


    //初始化音频
    private void initMediaPlayer() {
        try {
            //手机的根路径下
            File file = new File(Environment.getExternalStorageDirectory(), "music.mp3");
            mediaPlayer.setDataSource(file.getPath()); // 指定音频文件的路径
            mediaPlayer.prepare(); // 让MediaPlayer进入到准备状态
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.playMusic:
                if (!mediaPlayer.isPlaying()) {
                    mediaPlayer.start(); // 开始播放音频
                }
                break;
            case R.id.pauseMusic:
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause(); // 暂停播放音频
                }
                break;
            case R.id.stopMusic:
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.reset(); // 停止播放音频
                    initMediaPlayer();
                }
            case R.id.playVideo:
                if (!videoView.isPlaying()) {
                    videoView.start(); // 开始播放视频
                }
                break;
            case R.id.pauseVideo:
                if (videoView.isPlaying()) {
                    videoView.pause(); // 暂停播放视频
                }
                break;
            case R.id.replayVideo:
                if (videoView.isPlaying()) {
                    videoView.resume(); // 重新播放视频
                }
                break;
            default:
                break;
        }


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
        if (videoView != null) {
            videoView.suspend();
        }

    }


}
